Re: [GENERAL] Howto convert arrays 2 query results

Поиск
Список
Период
Сортировка
От Herouth Maoz
Тема Re: [GENERAL] Howto convert arrays 2 query results
Дата
Msg-id l03130301b384306b7c2a@[147.233.159.109]
обсуждение исходный текст
Ответ на Howto convert arrays 2 query results  (Jeroen Schaap <jeroen@rulffh.medfac.leidenuniv.nl>)
Ответы Re: [GENERAL] Howto convert arrays 2 query results
Список pgsql-general
At 10:13 +0300 on 09/06/1999, Jeroen Schaap wrote:


> Do you know of any way to generally convert arrays into query results?
>
> I know it is better to implement arrays as tables, but that results in
> unreadable tables (with 10 rows with id=1, 15 with id=2, 2 with id=3 ad
> infundum...).
>
> So is there any way to convert an array into a table? Should I
> write a function or a C-function?

It's not entirely clear what you want. The reason to keep arrays together
in a separate table is organizational. The way you want to present the
arrays shoud not affect the way they are organized.

If it bothers you that a query returns something like

id   person   child
===  =======  =======
1    Susan    Tom
1    Susan    Merry
1    Susan    Donna
2    George   Ben
2    George   Peggy
3    Morris   Elias

And you want it to show something like:

Person   Children
======   ========
Susan    Tom, Merry, Donna
George   Ben, Peggy
Morris   Elias

What you do is write it this way in the frontend. It depends on your
favourite frontend language, but the general algorithm should be something
along the lines of:

last_id = 0;

while ( still_more_tuples )

   get_next_tuple;

   if ( tuple.id = last_id )
       print( "," + tuple.child )
   else
       print( <newline> + tuple.person + <tab> + tuple.child )
   end if

   last_id = tuple.id;

end while

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma



В списке pgsql-general по дате отправления:

Предыдущее
От: Jeroen Schaap
Дата:
Сообщение: Howto convert arrays 2 query results
Следующее
От: Adriaan Joubert
Дата:
Сообщение: User-defined types and indices